package com.jnu.myfirstapplication;

import static com.jnu.myfirstapplication.BookListMainActivity.isDarkTheme;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import java.util.ArrayList;

public class Setting extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_setting);

        ImageButton back=findViewById(R.id.settings_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setResult(200);
                finish();
            }
        });


        Switch switch1= (Switch) findViewById(R.id.settings_dark);
        Switch switch2= (Switch) findViewById(R.id.settings_update);

        if (isDarkTheme(Setting.this)) {
            switch1.setChecked(true);
        }
        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                {
//                    View color=findViewById(R.color.colorPrimary)
                    Toast.makeText(Setting.this,"Now it have been changed to dark mode",Toast.LENGTH_SHORT).show();
//                    if (isDarkTheme(Setting.this)) {
//                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
//                    } else {
//                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
//                    }
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                }else{
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    Toast.makeText(Setting.this,"Now it have been changed back",Toast.LENGTH_SHORT).show();
                }
            }
        });

        switch2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                {
                    Toast.makeText(Setting.this,"Now it will check if there is any update when launch",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(Setting.this,"Now it won't check update",Toast.LENGTH_SHORT).show();

                }
            }
        });

//        OnClick onClick=new OnClick();
//        switch1.setOnClickListener(onClick);
//        switch2.setOnClickListener(onClick);

        RadioGroup group1=findViewById(R.id.settings_group1);
        group1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showMultiChoiceDialog();
            }
        });
    }
    class OnClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.settings_dark:
                    break;
                case R.id.settings_update:
                    Toast.makeText(Setting.this,"Now it will check if there is any update when launch",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }


    ArrayList<Integer> yourChoices = new ArrayList<>();

    private void showMultiChoiceDialog() {
        yourChoices.add(0);
        yourChoices.add(1);
        final String[] items = { "DouBan.com","OpenLibrary.com"};
        // 设置默认选中的选项，全为false默认均未选中
        final boolean initChoiceSets[]={true,true};
        yourChoices.clear();
        AlertDialog.Builder multiChoiceDialog =
                new AlertDialog.Builder(Setting.this);
        multiChoiceDialog.setTitle("Webs");
        multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
                new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which,
                                        boolean isChecked) {
//                        if (isChecked) {
//                            yourChoices.add(which);
//                        } else {
//                            yourChoices.remove(which);
//                        }
                    }
                });
        multiChoiceDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        int size = yourChoices.size();
//                        String str = "";
//                        for (int i = 0; i < size; i++) {
//                            str += items[yourChoices.get(i)] + " ";
//                        }
//                        Toast.makeText(Setting.this,
//                                "你选中了" + str,
//                                Toast.LENGTH_SHORT).show();
                    }
                });
        multiChoiceDialog.show();
    }
}